#coding:utf8
#
# Author: CMGS
# Created on 2010-10-13
#

from libs.klive import getApiData as _getApiData

#COOPER地址
COOPER_URL = 'http://127.0.0.1:8080/join'
#用户名
USERNAME = 'pengzhefu@kingsoft.com'
#用户密码
PASSWORD = '80704797'
#设备ID
DEVID = 'CMGS-TEST'
#设备名
CLIENTNAME = 'CMGS-PC'
#设备版本
CLIENTVERSION = '1.0.0'

FILEID = '2345309841657364'

def loginKlive(username, password, deviceId, clientName, clientVersion):
    params = {'user': username, 'password' : password, 'deviceId': deviceId,
              'clientName': clientName, 'clientVersion': clientVersion}
    parserResult = _getApiData('login', params, 2)
    assert (parserResult != {})
    print 'Token is %s' % parserResult['token']
    return parserResult['token']

def dirKlive(token):
    params = {'token': token, 'fileId': ''}
    parserResult = _getApiData('dir', params, 2)
    assert (parserResult != {})
    print parserResult

def testJoin(token, hash, sid = ''):
    import urllib2
    if sid == '':
        data = 'token=%s&hash=%s' % (token, hash)
    else:
        data = 'token=%s&hash=%s&sid=%s' % (token, hash, sid)
    req = urllib2.Request(COOPER_URL, data)
    res = urllib2.urlopen(req)
    print res.read()

def main():
    token = loginKlive(USERNAME, PASSWORD, DEVID, CLIENTNAME, CLIENTVERSION)
    testJoin(token, FILEID, '0000012ba61f43fa1da074e5000a00140080005f')

if __name__ == '__main__':
    main()
